package ui;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import obj.Player;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import play.Things;

import util.Settings;

/**
 * Data menu.
 * @author VincentLy
 */
public class DataMenu extends BasicGameState{

	private boolean nosave;
	private CustomButton load;
	private CustomButton back;	
	private String s1 = "Data";
	private String s2 = "World: ";
	private String s3 = "No previous save file.";
	private int world;
	private Scanner s;
	private float ex,ey = -1;
	
	public DataMenu(){
		super();
		load = new CustomButton(Settings.screenX/2-Settings.buttonX/2,300,"Load",'l');
		back = new CustomButton(Settings.screenX/2-Settings.buttonX/2,Settings.screenY-Settings.buttonY*2,"Back",'b');
	}
	
	/**
	 * Checks the file for a previous save.
	 * @author VincentLy
	 */
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
		try{
			s = new Scanner(new File("res/save.txt"));
			if(s.hasNext())
				world = s.nextInt();
			else
				nosave = true;	
		} 
		catch (FileNotFoundException e) {System.out.println("Load save problem.");}
	}

	/**
	 * Loads the data when the user clicks the button.
	 * @author VincentLy
	 */
	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
		Input i = gc.getInput();
		ex = i.getMouseX();
		ey = i.getMouseY();
		if(!nosave){
			if(load.contains(ex,ey)){
				load.setOn(true);
				if(i.isMousePressed(Input.MOUSE_LEFT_BUTTON)){
					try{
						s = new Scanner(new File("res/save.txt"));
						world = s.nextInt();
						Things.world = world;
						Things.file = "res/world" + world + "platforms.txt";
						Things.warps = s.nextInt();
						Things.x = s.nextFloat();
						Things.y = s.nextFloat();
						Things.player = new Player(Things.x,Things.y,1,Settings.player);
						Things.player.setHP(s.nextFloat());
						Things.gold = s.nextInt();
						sbg.getState(Settings.wID).init(gc,sbg);
						sbg.enterState(Settings.wID);
					} 
					catch (FileNotFoundException e) {System.out.println("Load save problem.");}
				}	
			}
			else
				load.setOn(false);
		}
		else 
			load.setOn(false);
		if(back.contains(ex,ey)){
			back.setOn(true);
			if(i.isMousePressed(Input.MOUSE_LEFT_BUTTON))
				sbg.enterState(Settings.smID);
		}	
		else
			back.setOn(false);	
	}
	
	/**
	 * Draws the text and buttons.
	 * @author VincentLy
	 */
	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		g.setAntiAlias(true);
		g.setBackground(Settings.bg);
		g.setLineWidth(1);
		g.setColor(Color.white);
		g.drawString(s1,(Settings.screenX-g.getFont().getWidth(s1))/2,g.getFont().getHeight(s1));
		if(nosave)
			g.drawString(s3,(Settings.screenX-g.getFont().getWidth(s3))/2,250);
		else
			g.drawString(s2 + world,(Settings.screenX-g.getFont().getWidth(s2+world))/2,250);
		back.draw(g);
		g.setLineWidth(1);
		if(!nosave)
			load.draw(g);
	}

	/**
	 * @return id
	 * @author VincentLy
	 */
	public int getID() {return Settings.dID;}	
}